function GetFullPath($BaseDir,$RelativeFile)
{
	[System.IO.FileInfo]$fileInfo = Join-Path $BaseDir $RelativeFile
	$fileInfo.FullName
}

function PsSyncAssureItemsDir($item,$WhatIf)
{
	if (-not $Whatif)
	{
		[system.io.directoryinfo]$testDir = Split-Path $item
		if (-not $testDir.Exists)
		{
			$dir = New-Item $testDir -Type Directory
		}
	}
}

function PsSyncCreateDir([string]$NewDirectoryPath,[switch]$Force=($false))
{
	if (Test-Path $NewDirectoryPath)
	{
		$DirItemCount = (dir $NewDirectoryPath).Count
		if ( $DirItemCount -gt 0 )
		{
			if (-not $force)
			{
				throw "$NewDirectoryPath contains items.  Delete or use -force"
			}
		}
		Write-Output "Delete Directory - $NewDirectoryPath FORCE RECURSE"
		Remove-Item -path $NewDirectoryPath -force -recurse 
	}
	
	New-Item $NewDirectoryPath -Type Directory
}

function PsSyncAddFile($RelativeFilePath)
{
	$SourcePath = $(GetFullPath $PsSyncSourceDir $RelativeFilePath)
	$DestPath = $(GetFullPath $PsSyncDestinationDir $RelativeFilePath)
	PsSyncAssureItemsDir $DestPath -WhatIf:$WhatIf
	
	Write-Output "Add File - $DestPath"
	
	Copy-Item $SourcePath $DestPath -Whatif:$Whatif

    if ($RollbackScript -ne $null)
    {
        "Remove-Item '$DestPath' -force" >> $RollbackScript
    }
}

function PsSyncUpdateFile($RelativeFilePath)
{
	$SourcePath = $(GetFullPath $PsSyncSourceDir $RelativeFilePath)
	$DestPath = $(GetFullPath $PsSyncDestinationDir $RelativeFilePath)

    if ($RollbackScript -ne $null)
    {
    	$RestorePath = $(GetFullPath $RollbackFiles $RelativeFilePath)
    	PsSyncAssureItemsDir $RestorePath -WhatIf:$WhatIf
		Write-Output "Backup File - $DestPath"
	    Copy-Item $DestPath $RestorePath -Whatif:$Whatif
    }

	PsSyncAssureItemsDir $DestPath -WhatIf:$WhatIf
	Write-Output "Update File - $DestPath"
	Copy-Item $SourcePath $DestPath -Whatif:$Whatif

    if ($RollbackScript -ne $null)
    {
        "Copy-Item '$RestorePath'  '$DestPath' -force" >> $RollbackScript
    }
}

function PsSyncDeleteFile($RelativeFilePath)
{
	$filePath = $(GetFullPath $PsSyncDestinationDir $RelativeFilePath)

    if ($RollbackScript -ne $null)
    {
    	$RestorePath = $(GetFullPath $RollbackFiles $RelativeFilePath)
    	PsSyncAssureItemsDir $RestorePath -WhatIf:$WhatIf
		Write-Output "Backup File - $filePath"
	    Copy-Item $filePath $RestorePath -Whatif:$Whatif
    }

	Write-Output "Delete File - $filePath"
	Remove-Item $filePath -Whatif:$Whatif

    if ($RollbackScript -ne $null)
    {
        "Copy-Item '$RestorePath' '$filePath -force" >> $RollbackScript
    }
}

function PsSyncRenameFile($RelativeSourceFilePath,$RelativeDestinationFilePath)
{
	$SourcePath = $(GetFullPath $PsSyncDestinationDir $RelativeSourceFilePath)
	$DestPath = $(GetFullPath $PsSyncDestinationDir $RelativeDestinationFilePath)
	PsSyncAssureItemsDir $DestPath -WhatIf:$WhatIf
	Write-Output "Rename File - '$SourcePath' to '$DestPath'"
	move-Item -path $SourcePath -destination $DestPath -Whatif:$Whatif

    if ($RollbackScript -ne $null)
    {
    	# PsSyncAssureItemsDir $SourcePath 
	    "move-Item -path '$DestPath' -destination '$SourcePath' -force" >> $RollbackScript
    }
}

function InitializeRollback
{
    if ($RollbackDir -eq $null)
    {
        $RollbackDir = ".\PsSyncRollback_{0:MM.dd.yy.HH.mm.ss}" -f  [datetime]::Now
    }
    else
    {
        if ( Test-Path $RollbackDir )
        {
            remove-item -Path $RollbackDir -recurse -force
        }
    }
    New-Item  $RollbackDir -ItemType Directory
}
